package simpleGeom;

import java.util.ArrayList;
import java.util.List;

import processing.core.PGraphics;

public class SimpleMesh {
public ArrayList<IsNode>nodes=new ArrayList<IsNode>();
ArrayList<Face3D>faces=new ArrayList<Face3D>();

public SimpleMesh constructMesh(){
	return new SimpleMesh();
}

public IsNode constructNode(float x,float y,float z){
	return new Node(x,y,z);
}

public Face3D constructFace(){
	return new Face3D();
}

public void addMesh(SimpleMesh mesh) {
	nodes.addAll(mesh.getNodes());
	faces.addAll(mesh.getFaces());
}
public ArrayList<IsNode> getNodes() {
	return nodes;
}
public void setNodes(List<? extends IsNode> nodes) {
	this.nodes = new ArrayList<IsNode>(nodes);
}
public ArrayList<Face3D> getFaces() {
	return faces;
}
public void setFaces(ArrayList<? extends Face3D> faces) {
	this.faces = (ArrayList<Face3D>) faces;
}
public int getNFaces() {
	return faces.size();
}

public Face3D getFace(int i){
	return faces.get(i);
}

public int getNNodes() {
	return nodes.size();
}

public IsNode getNode(int i){
	return nodes.get(i);
}

public void scale(float sX, float sY, float sZ) {
	for (int i = 0; i < getNNodes(); i++) {
		HasPos3D node = getNode(i);
		node.setPosition(node.getX()*sX,node.getY()*sY, node.getZ()*sZ);
	}
}
public void flipYZ(){
	for (int i = 0; i < getNNodes(); i++) {
		HasPos3D node = getNode(i);
		float temp=node.getY();
		node.setPosition(node.getX(),node.getZ(),temp );
	}
}

public void normalise(){
	normalise(1);
}

public void normalise(float dim){
	Bounds3D b3d=this.getBounds();
	float maxD = b3d.getMaxDimension();
	float scaleF =dim / maxD;
	translate(-b3d.getCenterX(), -b3d.getCenterY(),
			-b3d.getCenterZ());
	scale(scaleF, scaleF, scaleF);
}

public void normaliseToDim(float dim){
	Bounds3D b3d=this.getBounds();
	float maxD = b3d.getMaxDimension();
	float scaleF =dim / maxD;
	scale(scaleF, scaleF, scaleF);
}

public void translate(float sX, float sY, float sZ) {
	for (int i = 0; i < getNNodes(); i++) {
		HasPos3D node = getNode(i);
		node.setPosition(node.getX()+sX,node.getY()+sY, node.getZ()+sZ);
	}
}

public void display2D(PGraphics g) {
	// TODO Auto-generated method stub
	for (int i = 0; i < faces.size(); i++) {
		Face3D face = getFace(i);
		face.display2D(g);
	}
}
public void display3D(PGraphics g) {
	// TODO Auto-generated method stub
	for (int i = 0; i < faces.size(); i++) {
		Face3D face = getFace(i);
		face.display3D(g);
	}
}
public Bounds3D getBounds() {
	Bounds3D bounds = new Bounds3D();
	bounds.addAll(nodes);
	return bounds;
}
}
